package test.core.fun;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.yetus.audience.InterfaceAudience.Public;
import org.junit.jupiter.api.Test;

import hyl.core.MyFun;

public class testtime {
	public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	@Test
	public void 测试时间格式() {
		Date date = new Date();
		System.out.println(MyFun.date2Str(date, "yyMMddHHmmss"));
	} 
	public static String dateTransformBetweenTimeZone(Date sourceDate, DateFormat formatter, TimeZone sourceTimeZone,
			TimeZone targetTimeZone) {
		Long targetTime = sourceDate.getTime() - sourceTimeZone.getRawOffset() + targetTimeZone.getRawOffset();
		return getTime(new Date(targetTime), formatter);
	}

	public static String getTime(Date date, DateFormat formatter) {
		return formatter.format(date);
	}

	public static String convertDateBetweenTimeZone(String timeZone, Long createTime) {
		DateFormat dateFormat = new SimpleDateFormat("Z");
		String localTimeZone = dateFormat.format(new Date());
		TimeZone srcTimeZone = TimeZone.getTimeZone("GMT" + localTimeZone);
		TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);
		DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
		String destDateTime = dateTransformBetweenTimeZone(new Date(createTime), formatter, srcTimeZone, destTimeZone);
		return destDateTime;
	}

	// @Test
	public void test时间戳() {
		// 时间戳是计算机的一种计时方式，指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)
		// 起至现在的总秒数，每经过1秒自增1000，单位是毫秒
		Long datetime = System.currentTimeMillis();
		System.out.println(datetime);
		System.out.println(convertDateBetweenTimeZone("GMT+8", datetime));

	}

	// @Test
	public void test时间戳2() {
		// 时间戳是计算机的一种计时方式，指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)
		// 起至现在的总秒数，每经过1秒自增1000，单位是毫秒
		Long datetime = System.currentTimeMillis();
		System.out.println(datetime);
		Date date = new Date(datetime);
		System.out.println(getTime(date, "GMT+8"));

	}

//	@Test
	public void test中国时间1() {
		Calendar calendar = Calendar.getInstance();
		Date time = calendar.getTime();
		SimpleDateFormat data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = data.format(time);
		System.out.println(format);

	}

//	@Test
	public void test中国时间2() {
		Calendar calendar = Calendar.getInstance(Locale.CHINESE);
		Date time = calendar.getTime();
		SimpleDateFormat data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = data.format(time);
		System.out.println(format);

	}

	//@Test
	public void test设置时区() {
		TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
		TimeZone.setDefault(timeZone);
		Date time = new Date();
		SimpleDateFormat data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = data.format(time);
		print("设置时区",format);
	}

	// @Test
	public void test格林威治() {
		TimeZone timeZone1 = TimeZone.getDefault();// 获取当前服务器时区
		TimeZone timeZone2 = TimeZone.getTimeZone("Asia/Shanghai");// 获取上海时区
		TimeZone timeZone3 = TimeZone.getTimeZone("GMT");// 获取格林威治标准时区
		TimeZone timeZone4 = TimeZone.getTimeZone("GMT+8");// 获取东八区时区
		TimeZone timeZone5 = TimeZone.getTimeZone("UTC");// 获取UTC标准时间
		TimeZone timeZone6 = TimeZone.getTimeZone("CST");// 获取CST时区

		System.out.println(timeZone1.getRawOffset());
		System.out.println(timeZone2.getRawOffset());
		System.out.println(timeZone3.getRawOffset());
		System.out.println(timeZone4.getRawOffset());
		System.out.println(timeZone5.getRawOffset());
		System.out.println(timeZone6.getRawOffset());

	}

	//@Test
	public void 时间换算() {
		
		TimeZone timeZone1 = TimeZone.getTimeZone("GMT");// 获取格林威治标准时区
		TimeZone.setDefault(timeZone1);//设置程序的默认时区是 格林威治标准时区
		TimeZone timeZone2 = TimeZone.getTimeZone("GMT+8");// 获取东八区时区 (中国时间)
		int 时区差 = timeZone2.getRawOffset() - timeZone1.getRawOffset();
		Long datetime = System.currentTimeMillis(); //根据默认时区读取时间戳
		Long bjdatetime = datetime + 时区差; //获取北京时区的时间戳
		Date time = new Date(bjdatetime);//转成时间
		SimpleDateFormat data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = data.format(time);//按照指定格式输出
		System.out.println( "北京时间=" + format);
		print("时间换算", format);
	}

	public static void print(String tit, String msg) {
		System.out.println(tit + ":" + msg);
	}

	private static String getTime(Date date, String 时区) {
		if (时区 == null)
			时区 = "GMT+8";
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		dateFormat.setTimeZone(TimeZone.getTimeZone(时区));
		String BeijingTime = dateFormat.format(date);
		return BeijingTime;
	}
}
